﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.BLL;
using Jungle.Code.Utils;
using System.Collections.Generic;
using Jungle.Code.DAL;
using Jungle.Code.CategoryManager;

	public partial class Categories_Admin :   BaseAdminModule
	{
        string cmd = "";
        string id = "";
        string parentID = "";

        protected override void OnInit(EventArgs e)
        {
            if(Request["cmd"] != null)
                cmd = Request["cmd"];
            if (Request["cId"] != null)
                id = Request["cId"];
            if (Request["paretnID"] != null)
                parentID = Request["paretnID"];


            FillCategoryDdl(-1);

            switch (cmd)
            {
                case "view":                    
                    GlobalMenu.ActiveViewIndex = 1;
                    break;
                case "viewHidden":                    
                    GlobalMenu.ActiveViewIndex = 0;
                    break;
                case "add":
                    GlobalMenu.ActiveViewIndex = 2;
                    btn_AddNewMenuItem.Visible = true;
                    break;
                case "edit":
                    GlobalMenu.ActiveViewIndex = 2;
                    btn_SaveChangesMenuItem.Visible = true;
                    FillCategoryDdl(0);
                    break;
                case "move":
                    GlobalMenu.ActiveViewIndex = 3;                    
                    break;
                default:
                    GlobalMenu.ActiveViewIndex = 0;
                    break;
                
            }
            base.OnInit(e);
        }

        public override void FillBaseInfo()
        {
            sysName = "Categories";
        }
        public override void Main()
        {
        }
        #region Menu

        // --------------------------------------------------------------------------------------------
        public void FillCategoryDdl(int flag)
        {
            JungleTreeMenu jMenu = new JungleTreeMenu();

            jMenu.GetAll();

            ddl_MenuTreeItems.Items.Clear();
           

            foreach (JungleTreeMenu tm in jMenu.MenuItemsList)
            {
                ListItem li = new ListItem(tm.MenuText, tm.ID.ToString());
                ddl_MenuTreeItems.Items.Add(li);
                
            }
            if (flag != -1 && id != null)
            {
                JungleTreeMenu jMenuToEdit = new JungleTreeMenu();
                jMenuToEdit.ID = Convert.ToInt32(id);
                jMenuToEdit.GetById();
                txt_NewMenuItemText.Text = jMenuToEdit.MenuText;
                ddl_CategoryVisible.SelectedIndex = Convert.ToInt32(!jMenuToEdit.MenuVisible);
                ddl_MenuTreeItems.Visible = false;
                lbl_ParentMenu.Visible = false;
                btn_AddNewMenuItem.Visible = false;
            }
            else
            {
                ddl_MenuTreeItems.SelectedValue = "1";
            }

        }
        // --------------------------------------------------------------------------------------------
        public void AddMenuItem(object sender, EventArgs e)
        {
            JungleTreeMenu jMenuItem = new JungleTreeMenu();

            JungleTreeMenu jParentItem = new JungleTreeMenu();
            jParentItem.ID = Convert.ToInt32(ddl_MenuTreeItems.SelectedValue);
            jParentItem.GetById();

            if (txt_NewMenuItemText.Text != "")
            {
                jMenuItem.MenuText = txt_NewMenuItemText.Text;
                jMenuItem.MenuVisible = true;
                jMenuItem.ParentRightKey = jParentItem.RightKey;

                jMenuItem.ParentLevelId = jParentItem.LevelId;

                jMenuItem.AddMenuItem();

                Context.Response.Redirect("Modules.aspx?mod=Categories&cmd=view");
            }
            else
            {
                lbl_ResultAddCat.Text = "<strong>Название категории не может быть пустым</strong>";
            }


        }
        // --------------------------------------------------------------------------------------------
        public void DeleteAllMenuItems(object sender, EventArgs e)
        {
            JungleTreeMenu j = new JungleTreeMenu();
            j.DeletAllMenuItems();
        }
        // --------------------------------------------------------------------------------------------
        public void UpdateMenuItem(object sender, EventArgs e)
        {
            JungleTreeMenu jMenu = new JungleTreeMenu();
            if (id != null)
            {
                jMenu.ID = Convert.ToInt32(id);
                jMenu.GetById();
                if (ddl_CategoryVisible.SelectedIndex == 0) { jMenu.MenuVisible = true; } else { jMenu.MenuVisible = false; }
                jMenu.MenuText = txt_NewMenuItemText.Text;
                lbl_ResultAddCat.Text = "Изменения сохранены";
                jMenu.Update();
            }

        }
        // --------------------------------------------------------------------------------------------
        

        #endregion

	}
